const register = document.querySelector('.register') // 注册 盒子
const login = document.querySelector('.login') // 登录盒子

const registerA = register.querySelector('a') 
// console.log(registerA);
const loginA = login.querySelector('.login a')
// console.log(loginA);

const registerForm = register.querySelector('form')
const loginForm = login.querySelector('form')
// console.log(loginForm);
// 绑定点击事件
registerA.addEventListener('click',function(e){
    register.style.display = 'none'
    login.style.display = 'block'
})

loginA.addEventListener('click',function(e){
    register.style.display = 'block'
    login.style.display = 'none'
})

loginForm.addEventListener('submit',async function(e){
    e.preventDefault()
    const data = serialize(loginForm,{hash:true,empty:true})
    // console.log(data);
    const res = await axios({
        url:'/api/login',
        method:'post',
        data,
    })
    console.log(res);
    if (res.data.code === 0) {

        // console.log(res);
        
        localStorage.setItem('cms',res.data.token)
        toastr.success("登录成功")
        setTimeout(() => {
            location.href ='index.html'
        }, 1000);
        
    }
}) 

registerForm.addEventListener('submit',async function(e){
    e.preventDefault()
    const data = serialize(registerForm,{hash:true,empty:true})
    //  console.log(data);
     const res = await axios({
        url:'/api/register',
        method:'post',
        data,
    })
    if (res.data.code === 0) {
    // console.log(res);
        // localStorage.setItem('cms'.res.data.)

        toastr.success("注册成功")
        // 模拟点击事件
        registerA.click()
    }
})